#
# Copyright 2009, Jeffrey Palm.
#

# Instances of this class mimic iphones out there with which we'll be
# syncing up our itunes.  This is mainly for testing.
class IPhone < Common

  # This is the long id unique to every phone
  attr_reader :unique_id

  def initialize(unique_id,base_url=nil)
    super        base_url
    @unique_id = unique_id
  end


  # ----------------------------------------------------------------------
  # Implementation of Common
  # ----------------------------------------------------------------------

  # Returns info about the currently-playing song in itunes
  def read_info
    
  end

  # We are a phone, so we contact my server and request a new key
  def find_the_shared_key
    url = url('get_id.php') + '?user=' + CGI.escape(@unique_id)

    # Only want the last line
    lines = open_and_read(url).split /\n/
    @shared_key = lines[lines.length-1]
  end

  # Plays the track with id 'track_id' in itunes
  def play_track(track_id)

  end

  # Pauses player
  def pause_player

  end

  # Given the song 'info', try to find the matching track in our
  # itunes library and return that.
  def find_track(info)

    note 'Find track info:'
    note info_to_string info
    
    # Read the user library
    # XXX: This is the valid library on macs
    xml = @itunes.library

    # Right now we only care about macs and iphones
    if not File.exist? xml
      note 'Couldn\'t find itunes library at: ' + xml
      return nil
    end

    lines = open(xml).read.split /\n/

    # This is the actual track we're looking for, but, as you'll see,
    # we're cut some corners or matching an exact track
    note 'Looking for:'
    note ' artist : ' + info['artist'].to_s
    note ' album  : ' + info['album'].to_s
    note ' track  : ' + info['track'].to_s

    track_id = nil
    name     = nil
    artist   = nil
    album    = nil

    # Because it's easier we'll remove all quote from the song and
    # from the strings in the xml.  Create a new, imutable hash,
    # containing none of this nasty stuff.
    new_info = {}
    info.each do |k,v|
      next if not k # shouldn't happen
      next if not v # ok if it happens
      new_info[k] = remove_nasty_stuff v
    end
    info = new_info

    lines.each do |line|

      # <key>Track ID</key><integer>1141</integer>
      if not track_id
        line.scan /<key>Track ID<\/key><integer>(\d+)<\/integer>/ do |res|
          track_id = res[0]
        end
      end

      # <key>Artist</key><string>Sage Francis</string>
      if not artist
        line.scan /<key>Artist<\/key><string>([^<]+)<\/string>/ do |res|
          artist = res[0]
        end
      end
      # <key>Name</key><string>Hoofprints In the Sand</string>
      if not name
        line.scan /<key>Name<\/key><string>([^<]+)<\/string>/ do |res|
          name = res[0]
        end
      end

      # <key>Album</key><string>Human the Death Dance</string>
      if not album
        line.scan /<key>Album<\/key><string>([^<]+)<\/string>/ do |res|
          album = res[0]
        end
      end

      if track_id and name and artist and album

        debug 'Found a some track' if @verbose
        
        # Do an easy compare, that is...  
        #  - if we find an artist on this device, but none on the
        #    other, use that artist
        #  - if we find an artist and album on this device, but an
        #    artist not but album on the other, use that artist
        #  - if we find an artist and album and track on the other
        #    device, but only an artist and album on this, use this
        #    track
        if ( compare_leniently(info['artist']  , artist) &&
             compare_leniently(info['album']   , album ) &&
             compare_leniently(info['track']    , name )   )
          return Track.new name,artist,album,track_id
        end
        track_id  = nil
        name      = nil
        artist    = nil
        album     = nil
      end
    end
    return nil
  end
 
end
